package com.ideal.frame.node.service;

import com.ideal.frame.jpa.JpaRepositoryImpl;
import com.ideal.frame.node.entity.HeartbeatRootNodeTb;
import com.ideal.frame.node.entity.QHeartbeatRootNodeTb;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: xuyang
 * @date: 2021/8/27 16:39
 */
@Service
public class HeartbeatRootNodeService {
    @PersistenceContext
    protected EntityManager entityManager;

    private JpaRepositoryImpl<HeartbeatRootNodeTb, Long> heartbeatRepo;

    QHeartbeatRootNodeTb qHeartbeatRootNodeTb = QHeartbeatRootNodeTb.heartbeatRootNodeTb;

    @PostConstruct
    public void init() {
        this.heartbeatRepo = new JpaRepositoryImpl<>(HeartbeatRootNodeTb.class, entityManager);
    }

    public HeartbeatRootNodeTb getById(Long id){
        return this.heartbeatRepo.findById(id).orElse(null);
    }

    public HeartbeatRootNodeTb getByNodeId(String nodeId){
        HeartbeatRootNodeTb searchEx = new HeartbeatRootNodeTb();
        searchEx.setNodeid(nodeId);
        return this.heartbeatRepo.findOne(Example.of(searchEx)).orElse(null);
    }

    public List<HeartbeatRootNodeTb> getLastDayHearts(String nodeId){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime yesterday = now.minusDays(1);

        Date to = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        Date from = Date.from(yesterday.atZone(ZoneId.systemDefault()).toInstant());

        return heartbeatRepo.selectFrom(qHeartbeatRootNodeTb)
                .where(qHeartbeatRootNodeTb.nodeid.eq(nodeId).and(qHeartbeatRootNodeTb.insertdate.between(from, to)))
                .orderBy(qHeartbeatRootNodeTb.insertdate.asc())
                .fetch();
    }

    public List<HeartbeatRootNodeTb> getHeartsSchedule(String nodeId,Date lastDate){
        return heartbeatRepo.selectFrom(qHeartbeatRootNodeTb)
                .where(qHeartbeatRootNodeTb.nodeid.eq(nodeId).and(qHeartbeatRootNodeTb.insertdate.after(lastDate)))
                .orderBy(qHeartbeatRootNodeTb.insertdate.asc())
                .fetch();
    }


    @Transactional(rollbackFor = Exception.class)
    public void save(HeartbeatRootNodeTb info){
        // 新增
        if(info.getId() == null){
            info.setInsertdate(new Date());
            info.setUpdatedate(new Date());
            this.heartbeatRepo.save(info);
            return;
        }
        // 编辑
        HeartbeatRootNodeTb entityDB = this.getById(info.getId());
        Assert.notNull(entityDB, "id参数非法");
        entityDB.setUpdatedate(new Date());
        entityDB.setStatus(info.getStatus());
        this.heartbeatRepo.save(entityDB);
    }
}
